<template>
  <div>
    <el-dialog title="新增" :model-value="addDialogVisible" :before-close="handleCancel" width="30%">
      <el-form :model="formData" :rules="rules" label-width="80px" ref="addForm">
        <el-form-item label="中文名称:" prop="roleCnName">
          <el-input v-model="formData.roleCnName"></el-input>
        </el-form-item>
        <el-form-item label="英文名称:" prop="roleEnName">
          <el-input v-model="formData.roleEnName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup name='AddRole' lang='ts'>
import { ref, reactive } from 'vue'
import { addApi } from '@/api/roleApi';
import { ElMessage } from 'element-plus';
// 新增对应的字段
let formData = reactive({ roleCnName: '', roleEnName: '' })
let rules = reactive({
  roleCnName: [
    { required: true, message: '请输入角色中文名称', trigger: 'blur' }
  ],
  roleEnName: [
    { required: true, message: '请输入角色英文名称', trigger: 'blur' }
  ]
})

let addForm = ref()
defineProps<{ addDialogVisible: boolean }>()
let emitter = defineEmits(["closeDialog", "addSuccess"])

function handleCancel() {
  addForm.value.resetFields()
  emitter("closeDialog")
}
// 添加权限
function onSubmit() {
  addForm.value.validate((valid: boolean) => {
    if (!valid) return
    // 执行添加逻辑
    addApi(formData).then(result => {
      ElMessage({ type: "success", message: result.data })
      addForm.value.resetFields()
      emitter("addSuccess")
    })
  })
}
</script>
<style scoped></style>